package com.findtech.threePomelos.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.adapter.AboutAdapter;
import com.findtech.threePomelos.adapter.AdapterItemDecoration;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.entity.BabyInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.RequestUtils;
import com.findtech.threePomelos.utils.ToastUtil;
import com.findtech.threePomelos.view.dialog.CustomDialog;

import java.util.ArrayList;

public class MyDeviceOperateActivity extends MyActionBarActivity implements RequestUtils.MyItemClickListener {

    private AboutAdapter mAdapter;
    private String bind;
    private String unbind;
    private String firmwareVersion;
    private String currentElectric;
    private ArrayList<String> listItem = new ArrayList<>();
    private BabyInfoEntity babyInfoEntity = BabyInfoEntity.getInstance();
    private NetWorkRequest netWorkRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_device_operate);
        MyApplication.getInstance().addActivity(this);
        setToolbar("三个柚子推车", true);
        bind = getResources().getString(R.string.app_device_text_bind);
        unbind = getResources().getString(R.string.app_device_text_unbind);
        firmwareVersion = getResources().getString(R.string.app_device_text_check_firmware_version);
        currentElectric = getResources().getString(R.string.app_device_text_current_electric);
        mAdapter = new AboutAdapter(this);
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.app_device_operate_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new AdapterItemDecoration(this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        netWorkRequest = new NetWorkRequest(this);
        listItem.add(bind);
        listItem.add(unbind);
        listItem.add(firmwareVersion);
        listItem.add(currentElectric);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAdapter.setListItem(listItem);
        String firmwareVersion = RequestUtils.getSharepreference(this).getString(RequestUtils.FIRMWARE_VERSION, "");
        String currentElectric = RequestUtils.getSharepreference(this).getString(RequestUtils.CURRENT_ELECTRIC, "");
        if (!TextUtils.isEmpty(firmwareVersion)) {
            mAdapter.setFirmwareVersion("v" + firmwareVersion);
        }
        if (!TextUtils.isEmpty(currentElectric)) {
            mAdapter.setCurrentElectric(currentElectric + "%");
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case 0:
                if (babyInfoEntity.getIsBind()) {
                    final CustomDialog.Builder builder = new CustomDialog.Builder(this);
                    builder.setTitle("提示");
                    builder.setShowBindInfo("解除绑定后,才能再次绑定推车!");
                    builder.setShowButton(true);
                    builder.setPositiveButton("解除绑定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            netWorkRequest.saveBlueToothIsBind(false, "", null);
                            ToastUtil.showToast(MyDeviceOperateActivity.this, "解除绑定");
                            dialog.dismiss();
                        }
                    });

                    builder.setNegativeButton("取消",
                            new android.content.DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                } else {
                    Intent intent = new Intent(this, BindCarActivity.class);
                    intent.putExtra("from", "MyDeviceOperateActivity");
                    startActivity(intent);
                }
                break;
            case 1:
                final CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle("解除绑定");
                if (babyInfoEntity.getIsBind()) {
                    builder.setShowBindInfo("解除绑定后数据不能进行同步");
                } else {
                    builder.setShowBindInfo("没有绑定三个柚子智能婴儿车");
                }

                builder.setShowButton(true);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (babyInfoEntity.getIsBind()) {
                            netWorkRequest.saveBlueToothIsBind(false, "", null);
                            Log.d("ZZ", "MyDeviceOperateActivity disconnectedDevice = " + app.manager.cubicBLEDevice);
                            if (app.manager.cubicBLEDevice != null) {
                                try {
                                    app.manager.cubicBLEDevice.disconnectedDevice();
                                } catch (Exception e) {
                                    Log.d("ZZ", "SearchFragment disconnectedDevice e = " + e);
                                }
                                app.manager.cubicBLEDevice = null;
                            }
                            ToastUtil.showToast(MyDeviceOperateActivity.this, "解除绑定");
                        }
                        dialog.dismiss();
                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            case 2:
//                final CustomDialog.Builder builder1 = new CustomDialog.Builder(this);
//                builder1.setTitle(getResources().getString(R.string.title_dialog_firmware_update));
//                if (true) {
//                    builder1.setShowBindInfo(getResources().getString(R.string.message_dialog_firmware_update));
//                } else {
//                    builder1.setShowBindInfo(getResources().getString(R.string.message_dialog_firmware_no_update));
//                }
//
//                builder1.setShowButton(true);
//                builder1.setPositiveButton(getResources().getString(R.string.ok_button_dialog_firmware_update), new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        startActivity(new Intent(MyDeviceOperateActivity.this, FirmwareUpdateActivity.class));
//                        dialog.dismiss();
//                    }
//                });
//
//                builder1.setNegativeButton(getResources().getString(R.string.cancel_button_dialog_firmware_update),
//                        new android.content.DialogInterface.OnClickListener() {
//                            public void onClick(DialogInterface dialog, int which) {
//                                dialog.dismiss();
//                            }
//                        });
//                builder1.create().show();
        }
    }

}
